#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    //ui->lineEdit->setText("/");

    //&QLineEdit::returnPressed --》lineedit的回车事件
    QDir rootDir("./");  //设置当前路径为根目录
    QStringList strList;
    strList << "*";

    //entryInfoList用于获取文件夹下面的文件列表
    QFileInfoList list = rootDir.entryInfoList(/*strList*/);

    displayFun(list);
}

void Dialog::displayFun(QFileInfoList& list)
{
    ui->listWidget->clear();

    for(int i = 0; i < list.size(); i++)
    {
        QFileInfo fileInfo = list[i];

        if(fileInfo.isDir())
        {
            QIcon ico("c:/image/dir.jpg");
            QString filename = fileInfo.fileName();
            ui->listWidget->addItem(new QListWidgetItem(ico, filename));
        }
        else if(fileInfo.isFile())
        {
            QIcon ico("c:/image/file.jpg");
            QString filename = fileInfo.fileName();
            ui->listWidget->addItem(new QListWidgetItem(ico, filename));
        }
    }
}




Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_lineEdit_returnPressed()
{
   QString str = ui->lineEdit->text();
   QDir dir(str);

   QFileInfoList list = dir.entryInfoList();

   displayFun(list);
}


void Dialog::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    QDir dir;
    QString str = item->text();

    dir.setPath(ui->lineEdit->text());
    dir.cd(str);

    ui->lineEdit->setText(dir.absolutePath()); //设置相对路径

    QFileInfoList list = dir.entryInfoList();

    displayFun(list);
}
